﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using log4net;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;
using Exchange.Business;
namespace liveatedu
{
    public partial class OutlookMail : System.Web.UI.UserControl
    {
        private hnt_ExchangeWebService exchange = null;
        private int countEmail = 5;
        private int countUnRead = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            exchange = new hnt_ExchangeWebService();
        

            //pnlLogin.Visible = true;
            //pnlEchange.Visible = false;
            if (CookieHelper.CookieExists("UserOutlookMail"))
            {
                loaddata();
            }
        }
        private ExchangeService GetService()
        {
            ExchangeService service = null;
            try
            {
                string email = "";
                string password = "";

                string userEmail = CookieHelper.GetSecureCookieValue("UserOutlookMail");
                string[] strUser = userEmail.Split('|');
                email = strUser[0];
                password = strUser[1];

                service = exchange.CreateExchangeService(email, password);
                Session["Service"] = service;
            }
            catch (Exception) { }

            return service;
        }

        private void loaddata()
        {
            //load Mail
            if (Session["Service"] != null)
            {
                ExchangeService service = (ExchangeService)Session["Service"];
                loadTableEmail(service);
                pnlLogin.Visible = false;
                pnlEchange.Visible = true;

            }
            else
            {
                ExchangeService service = GetService();
                loadTableEmail(service);


            }
            if (countUnRead > 0)
            {
                hplInboxMail.Text += " (" + countUnRead + ")";
            }

        }

        protected void lbtLogout_Click(object sender, EventArgs e)
        {
            RemoveCookie();

            pnlLogin.Visible = true;
            pnlEchange.Visible = false;
        }

        private void RemoveCookie()
        {
            HttpCookie userOutlookMail = new HttpCookie("UserOutlookMail", string.Empty);
            userOutlookMail.Expires = DateTime.Now.AddSeconds(1);
            userOutlookMail.Path = "/";
            Response.Cookies.Add(userOutlookMail);
            Session.Remove("Service");
        }

        protected void lbtRefresh_Click(object sender, EventArgs e)
        {
            ExchangeService service = GetService();
            loadTableEmail(service);
        }
        private void loadTableEmail(ExchangeService service)
        {
            try
            {
                DataTable dtEmail = exchange.GetMail(service, countEmail);
                dtlLiveMail.DataSource = dtEmail;
                dtlLiveMail.DataBind();

                foreach (DataRow dtr in dtEmail.Rows)
                {
                    bool isRead = (bool)dtr["IsRead"];
                    if (!isRead)
                    {
                        countUnRead += 1;
                    }
                }
                //Session["TableOutlookMail"] = dtEmail;
                pnlLogin.Visible = false;
                pnlEchange.Visible = true;
            }
            catch (Exception)
            {
                //Session.Remove("TableOutlookMail");

            }


        }

        protected string FormatHeading(string strHeading, string strIsRead)
        {
            bool isRead = bool.Parse(strIsRead);
            if (!isRead)
            {
                strHeading = "<strong>" + strHeading + "</strong>";
            }
            return strHeading;
        }
        protected string FormatSenđate(string dateSend)
        {
            dateSend = "(" + dateSend + ")";
            return dateSend;
        }
    }
}